﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;


namespace Nivdal
{
    public partial class EditForumPost : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.EditForumPost_PageTitle;
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            //this.Title = Resources.Resource.EditForumPost_Title + " - " + GND.ReturnAppTitle();
            OnlineUser ou = (OnlineUser)Session["User"];

            

            string PostId = null;

            if (Request.QueryString["PostId"] != null)
            {
                PostId = Request.QueryString["PostId"].ToString();
                PostIdHiddenField.Value = PostId;
            }
            else
            {
                Response.Redirect("~/ForumHomePage.aspx");
            }

            if (!GND.ReadNivdalBoolData("@PostId", "@UserId", "Nivdal_ForumCanUserEditPost", PostId, ou.UserId))
            {
                Response.Redirect("~/ForumHomePage.aspx");
            }

            if (ou.TeamId == "Null")
                Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

            //if (ou.IsRTL())
            //{
            //    const string crlf = "\r\n";
            //    string js = "<script type='text/javascript'>" + crlf +
            //        "{" + crlf +
            //            "<script type=text/javascript>" + crlf +

            //    "Sys.Application.add_load(function () {" + crlf +

            //        "var Threadeditor = $find(<%= Threadeditor.ClientID %>);" + crlf +
            //        "var editPanel = Threadeditor.get_editPanel();" + crlf +

            //        "editPanel._setActive_saved = editPanel._setActive;" + crlf +
            //        "editPanel._setActive = function () {" + crlf +
            //            "if (this.get_activeMode() == AjaxControlToolkit.HTMLEditor.ActiveModeType.Design) {" + crlf +
            //                "var designPanel = this.get_activePanel();" + crlf +
            //                "if (this._eAfter == null) {" + crlf +
            //                    "designPanel._doc.body.style.direction = rtl;" + crlf +
            //                "}" + crlf +
            //            "}" + crlf +
            //            "this._setActive_saved();" + crlf +
            //        "};" + crlf +
            //    "});" + crlf +
            //    "</script>";
            //    Page.RegisterStartupScript("js", js);

            //}

            // TextHelpLabel.Text = GlobalFunctions.EditorHelp();

            if (!IsPostBack)
                this.Threadeditor.Text = (GND.ReadNivdalStringData("@PostId", "Nivdal_ForumGetPostText", PostId));
                this.ForumNameLabel.Text = GND.ReadNivdalStringData("@PostId", "Nivdal_ForumGetForumNameByPost", PostId);
                this.ThreadNameLabel.Text = GND.ReadNivdalStringData("@PostId", "Nivdal_ForumGetThreadNameByPost", PostId);

        }

        protected void SubmitChanges_Click(object sender, EventArgs e)
        {
            this.MessageLabel.Text = "";
            this.MessageLabel.ForeColor = System.Drawing.Color.Green;
            string PostId = PostIdHiddenField.Value;
            
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];

            string ThreadId = (GND.ReadNivdalIntData("@PostId", "Nivdal_ForumGetThreadByPost", PostId));

            String PostText = this.Threadeditor.Text.Trim();
            if (PostText.Length > 4000 || PostText.Length < 1)
            {
                this.MessageLabel.ForeColor = System.Drawing.Color.Red;
                this.MessageLabel.Text = (Resources.Resource.WriteForumPost_ErrorMessage);
            }
            else //input is good
            {

                string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
                System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
                dbCommand.CommandText = "Nivdal_ForumUpdatePost";
                dbCommand.CommandType = CommandType.StoredProcedure;
                dbCommand.Connection = dbConnection;

                SqlParameter dbParam_PostId = new SqlParameter();
                dbParam_PostId.ParameterName = "@PostId";
                dbParam_PostId.Value = PostId;
                dbParam_PostId.DbType = DbType.String;
                dbCommand.Parameters.Add(dbParam_PostId);

                SqlParameter dbParam_PostText = new SqlParameter();
                dbParam_PostText.ParameterName = "@Text";
                dbParam_PostText.Value = GlobalFunctions.EncodeHtmlFromText(PostText);
                dbParam_PostText.DbType = DbType.String;
                dbCommand.Parameters.Add(dbParam_PostText);
                
                SqlParameter dbParam_OriginalPostText = new SqlParameter();
                dbParam_OriginalPostText.ParameterName = "@OriginalText";
                dbParam_OriginalPostText.Value = PostText;
                dbParam_OriginalPostText.DbType = DbType.String;
                dbCommand.Parameters.Add(dbParam_OriginalPostText);

                

                SqlParameter dbParam_Answer = new SqlParameter();
                dbParam_Answer.ParameterName = "@Answer";
                dbParam_Answer.Value = "";
                dbParam_Answer.DbType = DbType.String;
                dbParam_Answer.Direction = ParameterDirection.Output;
                dbParam_Answer.Size = 64;
                dbCommand.Parameters.Add(dbParam_Answer);


                SqlParameter dbParam_Language = new SqlParameter();
                dbParam_Language.ParameterName = "@Language";
                dbParam_Language.Value = ou.Language;
                dbParam_Language.DbType = DbType.Decimal;
                dbCommand.Parameters.Add(dbParam_Language);

                try
                {

                    dbConnection.Open();
                    dbCommand.ExecuteScalar();
                    Response.Redirect("ThreadDetails.aspx?ThreadId=" + ThreadId + "&PostId=" + PostId + "&Answer=" + dbParam_Answer.Value.ToString());
                }
                catch (Exception SQLDBException)
                {
                    this.MessageLabel.Text = Convert.ToString(SQLDBException);
                }

                finally
                {
                    dbConnection.Close();
                }
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            this.PreviewLabel.Text = GlobalFunctions.EncodeHtmlFromText(this.Threadeditor.Text.Trim());
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }
    }
}